/*
Client session process
add by szz
*/
#ifndef _NETWORK_CLIENT_SESSION_H_
#define _NETWORK_CLIENT_SESSION_H_

#include "Common.h"
#include "AsyncComm.h"


class DataPacket;
class Session
{
protected:
	std::string m_userName,m_passHash,m_address;
	uint32	m_nPort;

public:
	enum AsyncCommType
	{
		eInvalid,
		eTCP,
		eSerial,
		eTypeCount
	};

	const std::string & GetUserName() { return m_userName; };

	//defaut
	Session();
	virtual ~Session();
	virtual bool InitSession(const char* pszAddress, const char* pszUserName, const char* pszPassHash,AsyncCommType type =  eSerial);
	virtual bool FiniSession();
	virtual bool Update(int32 deltaTime);

	virtual bool HandlePacket(DataPacket* packet) = 0;
	virtual void OnConnect(int32 errorCode);
	virtual void OnDisconnect(int32 errorCode);
	virtual void InitPacketHandlerTable();


	//packet send/recv
	void QueuePacket(DataPacket* packet);
	void SendPacket(DataPacket& packet);
	void OutPacket(uint16 opcode);
	void OutPacket(uint16 opcode, uint16 len, const void* data);
	bool IsConnectionOk();

	bool SendBData(const uint8* pData, uint8 nLen);
	
private:
	void InitClientSession();
	bool UpdateClientSession(int32 deltaTime);

protected:
	uint32 m_lastPong;
	bool	m_bConnected;
	std::queue<DataPacket*> m_msgQueue;
	AsyncComm* m_pComm;
	
};

#endif
